
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pyton 模拟Post登录 - 森语音 - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=ON3Mxdo4-HlSMqbNDBZXhFIcGLon3eZDvU8zBESgwkk1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/LoveGiveHeart/bundle-LoveGiveHeart.css?v=IGMc4CN0aLftzobhhXGr5TuDjD15FgLyFMLQh4VIHZ01"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/131517.css?v=Dh3K3lMG8ygZwd0TvZL1xYyMcek%3d"/>
<link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="/skins/LoveGiveHeart/bundle-LoveGiveHeart-mobile.css?v=d9LctKHRIQp9rreugMcQ1-UJuq_j1fo0GZXTXj8Bqrk1"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/593213556wuyubao/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/593213556wuyubao/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/593213556wuyubao/wlwmanifest.xml"/>
<script src="//common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script>
<script type="text/javascript">var currentBlogApp = '593213556wuyubao', cb_enable_mathjax=false;var isLogined=false;</script>
<script src="/bundles/blog-common.js?v=O-NTEmnhjbG7lSYLc3yeqkrVxfMJyY9iXf4xyjlKikw1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>
<!--PageBeginHtml Block Begin-->
<center style="color:red;"><h1>闲来敲敲代码......</h1></center>
<a  class="btn btn_demo1" onclick='if(this.innerHTML=="Click me!")this.innerHTML="you can!";else{this.innerHTML="Click me!";}'>Click me!</a>
<!--PageBeginHtml Block End-->

<!--done-->
<div id="banner">
<!--done-->
<div class="header">
	<div class="headerText">
		<a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/593213556wuyubao/">有容乃大：好读书不求甚解</a><br>
		<div class="headerDis"></div>
	</div>
</div>
</div>
<div id="main">
	<!-- right starts -->
	<div id="right">
		<div id="right_content">
			<div id="mylinks">
				<DIV id="mystats">
					
						<div id="blog_stats">
<!--done-->
<div class="blogStats">
随笔- 58&nbsp;
文章- 0&nbsp;
评论- 6&nbsp;

</div></div>
					
				</DIV>
				<a id="blog_nav_sitehome" class="menu" href="http://www.cnblogs.com/">博客园</a>&nbsp;&nbsp;<a id="blog_nav_myhome" class="menu" href="http://www.cnblogs.com/593213556wuyubao/">首页</a>&nbsp;&nbsp;<a id="blog_nav_newpost" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a>&nbsp;&nbsp;<a id="blog_nav_contact" accesskey="9" class="menu" rel="nofollow" href="https://msg.cnblogs.com/send/%E6%A3%AE%E8%AF%AD%E9%9F%B3">联系</a>&nbsp;&nbsp;<a id="blog_nav_admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">管理</a>&nbsp;&nbsp;<a id="blog_nav_rss" class="menu" href="http://www.cnblogs.com/593213556wuyubao/rss">订阅</a>&nbsp;<a id="blog_nav_rss_image" href="http://www.cnblogs.com/593213556wuyubao/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="订阅" /></a>
			</div>
			<div id="topics">
				
<div id="post_detail">
<!--done-->
<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/593213556wuyubao/p/4917789.html">Pyton 模拟Post登录</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><div class="cnblogs_code">
<pre><span style="color: #0000ff;">import</span><span style="color: #000000;"> sys
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> urlib.parse
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> urllib.request
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> http.cookiejar
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> random
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> math
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> os.path
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> hashlib
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> json
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> binascii
</span><span style="color: #008000;">#</span><span style="color: #008000;">cookie</span>
cookie=<span style="color: #000000;">http.cookiejar.LWPCookieJar()
chandle</span>=<span style="color: #000000;">urllib.request.HTTPCookieProcessor(cookie)

</span><span style="color: #008000;">#</span><span style="color: #008000;">get data</span>
<span style="color: #0000ff;">def</span><span style="color: #000000;"> getData(url):
    r</span>=<span style="color: #000000;">urllib.request.Request(url)
    opener</span>=<span style="color: #000000;">urllib.request.build_opener(chandle)
    u</span>=<span style="color: #000000;">opener.open(r)
    data</span>=<span style="color: #000000;">u.read()
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;">:
        data</span>=data.decode(<span style="color: #800000;">'</span><span style="color: #800000;">utf-8</span><span style="color: #800000;">'</span><span style="color: #000000;">)
    </span><span style="color: #0000ff;">except</span><span style="color: #000000;">:
        data</span>=data.decode(<span style="color: #800000;">'</span><span style="color: #800000;">gbk</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">ignore</span><span style="color: #800000;">'</span><span style="color: #000000;">)
    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> data
    
</span><span style="color: #008000;">#</span><span style="color: #008000;">post data</span>
<span style="color: #0000ff;">def</span><span style="color: #000000;"> postData(url,data):
    data</span>=<span style="color: #000000;">urllib.parse.urlencode(data)
    data</span>=bytes(data,<span style="color: #800000;">'</span><span style="color: #800000;">utf-8</span><span style="color: #800000;">'</span><span style="color: #000000;">)
    r</span>=<span style="color: #000000;">urllib.request.Request(url,data)
    opener</span>=<span style="color: #000000;">urllib.request.build_opener(chandle)
    u</span>=<span style="color: #000000;">opener.open(r)
    
    data</span>=<span style="color: #000000;">u.read()
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;">:
        data</span>=data.decode(<span style="color: #800000;">'</span><span style="color: #800000;">utf-8</span><span style="color: #800000;">'</span><span style="color: #000000;">)
    </span><span style="color: #0000ff;">except</span><span style="color: #000000;">:
        data</span>=data.decode(<span style="color: #800000;">'</span><span style="color: #800000;">gbk</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">ignore</span><span style="color: #800000;">'</span><span style="color: #000000;">)
    </span><span style="color: #0000ff;">return</span> data<br /><br />　　　　</pre>
</div>
<p>&nbsp;</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>


		</div>
		<div class = "postDesc">posted @ <span id="post-date">2015-10-28 16:42</span> <a href='http://www.cnblogs.com/593213556wuyubao/'>森语音</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=4917789" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(4917789);return false;">收藏</a></div>
	</div>
	<script type="text/javascript">var allowComments=true,cb_blogId=131517,cb_entryId=4917789,cb_blogApp=currentBlogApp,cb_blogUserGuid='ded6d7d9-d41c-e211-aa8f-842b2b196315',cb_entryCreatedDate='2015/10/28 16:42:00';loadViewCount(cb_entryId);var cb_postType=1;</script>
	
</div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id='comment_form' class='commentform'>
<a name='commentform'></a>
<div id='divCommentShow'></div>
<div id='comment_nav'><span id='span_refresh_tips'></span><a href='javascript:void(0);' onclick='return RefreshCommentList();' id='lnk_RefreshComments' runat='server' clientidmode='Static'>刷新评论</a><a href='#' onclick='return RefreshPage();'>刷新页面</a><a href='#top'>返回顶部</a></div>
<div id='comment_form_container'></div>
<div class='ad_text_commentbox' id='ad_text_under_commentbox'></div>
<div id='ad_t2'></div>
<div id='opt_under_post'></div>
<div id='cnblogs_c1' class='c_ad_block'></div>
<div id='under_post_news'></div>
<div id='cnblogs_c2' class='c_ad_block'></div>
<div id='under_post_kb'></div>
<div id='HistoryToday' class='c_ad_block'></div>
<script type='text/javascript'>
    fixPostBody();
    setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
    deliverAdT2();
    deliverAdC1();
    deliverAdC2();    
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);   
</script>
</div>


			</div>
			<div id="footer">
				
<!--done-->
Copyright &copy;2018 森语音
			</div>
		</div>
	</div>
	<!-- right ends -->
	<!-- left starts -->
	<div id="left">
		<!-- 左侧工具部分 -->
		<div id="left_content">
			
<!--done-->
<!-- <h1 class="listtitle">公告</h1> -->
<div class="newsItem">
	<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div>

			<div id="calendar"><div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script></div>
			
				<DIV id="leftcontentcontainer">
					<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
				</DIV>
			
		</div>
		<!-- //右侧工具部分 -->
	</div>
	<!-- left ends -->	
	<div class="clear"></div>
</div>

</body>
</html>
